<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<?php $this->load->view('head'); ?>
	<script type="text/javascript">   
            
                function correctTimeForDatepicker(id) {
//                    var rangeDemoConv = new AnyTime.Converter({format:"%z-%m-%d %H:%i", firstDOW: 1, stepMinute: 10});
//                    var f15min = 15 * 60 * 1000;
//                    var t = rangeDemoConv.parse(jQuery("#" + id).val()).getTime();
//                    if(t % f15min != 0) {
//                        t += (f15min - (t % f15min));
//                        jQuery("#" + id).val(rangeDemoConv.format(new Date(t)));
//                    }
                }
		jQuery(document).ready(function() {
			//jQuery("input[name=startdate]").datetimepicker({timeFormat: 'hh:mm', dateFormat: 'yy-mm-dd'});                                                              
                        
			var rangeDemoConv = new AnyTime.Converter({format:"%z-%m-%d %H:%i", firstDOW: 1});
			var oneDay = 24*60*60*1000;
			AnyTime.picker( "startdate",
				{ earliest: new Date(), format: "%z-%m-%d %H:%i", firstDOW: 1 } );
			AnyTime.picker( "enddate",
				{ format: "%z-%m-%d %H:%i", firstDOW: 1 } );
			jQuery("#startdate").change( function(e) { try {
                                correctTimeForDatepicker("startdate");
				var fromDay = rangeDemoConv.parse(jQuery("#startdate").val()).getTime();
				var threehours = 3*60*60*1000;
				if ((jQuery("#enddate").val() == "") || (jQuery("#enddate").val() != "" && ((rangeDemoConv.parse(jQuery("#startdate").val()).getTime() > rangeDemoConv.parse(jQuery("#enddate").val()).getTime()) || (rangeDemoConv.parse(jQuery("#enddate").val()).getTime() - threehours < rangeDemoConv.parse(jQuery("#startdate").val()).getTime())))) {
					var dayLater = new Date(fromDay+oneDay);
					jQuery("#enddate").
						AnyTime_noPicker().
						removeAttr("disabled").
						val(rangeDemoConv.format(new Date(fromDay + threehours))).
						AnyTime_picker(
							{
								earliest: new Date(fromDay + threehours),
								format: "%z-%m-%d %H:%i",
								firstDOW: 1
							} );
				} else {
					var oldTo = rangeDemoConv.parse(jQuery("#enddate").val()).getTime();
					
					jQuery("#enddate").
						AnyTime_noPicker().
						removeAttr("disabled").
						val(rangeDemoConv.format(new Date(oldTo))).
						AnyTime_picker(
							{
								earliest: new Date(fromDay),
								format: "%z-%m-%d %H:%i",
								firstDOW: 1
							} );
                                                        
                                        var d1 = rangeDemoConv.parse(jQuery("#startdate").val()).getTime();
                                        var d2 = rangeDemoConv.parse(jQuery("#enddate").val()).getTime();
                                        var threehours = 3 * 60 * 60 * 1000;
                                        if((d2 - d1) < threehours) {
                                            var adayLater = new Date(d1 + threehours);
                                            jQuery("#enddate").val(rangeDemoConv.format(new Date(adayLater))); 
                                            var StartDate = jQuery("input[name=startdate]").val();
                                            var EndDate = jQuery("input[name=enddate]").val();

                                            if (StartDate != "" && EndDate != "") {
                                                    jQuery("input[name=hours]").val(timeBetweenDates(StartDate, EndDate));
                                            }
                                        }
				}
			} catch(e){ jQuery("#enddate").val("").attr("disabled","disabled"); } } );
                    
			jQuery("input[name=startdate], input[name=enddate]").change(function() {
                                correctTimeForDatepicker("enddate");
				var StartDate = jQuery("input[name=startdate]").val();
				var EndDate = jQuery("input[name=enddate]").val();
				
				if (StartDate != "" && EndDate != "") {
					jQuery("input[name=hours]").val(timeBetweenDates(StartDate, EndDate));
				}
			});
			//jQuery("input[name=locations]").geo_autocomplete({geocoder_address: true, geocoder_types: 'street_address,locality,political,sublocality,neighborhood,country'});
			jQuery("input[name=locations]").geo_autocomplete({geocoder_address: true, geocoder_region: 'SG', geocoder_types: 'street_address,route,intersection,political,country,administartive_area_level_1,administartive_area_level_2,administartive_area_level_3,colloquial_area,locality,sublocality,neighborhood,premise,subpremise,postal_code,natural_feature,airport,park,point_of_intereset,post_box,street_number'});
                        
                        jQuery("#enddate").focusout(function() {
                            var d1 = rangeDemoConv.parse(jQuery("#startdate").val()).getTime();
                            var d2 = rangeDemoConv.parse(jQuery("#enddate").val()).getTime();
                            var threehours = 3 * 60 * 60 * 1000;
                            if((d2 - d1) < threehours) {
                                var adayLater = new Date(d1 + threehours);
                                jQuery("#enddate").val(rangeDemoConv.format(new Date(adayLater)));                                
                                var StartDate = jQuery("input[name=startdate]").val();
				var EndDate = jQuery("input[name=enddate]").val();
				
				if (StartDate != "" && EndDate != "") {
					jQuery("input[name=hours]").val(timeBetweenDates(StartDate, EndDate));
				}
                            }
                        });
                        
		});
	</script>
</head>

<body>
	<?php
	if ($user->id > 0) {
		$this->load->view('header_logged.php');
	} else {
		$this->load->view('header_guest.php');
	}
	?>
	
	<!-- Content Area -->
	<div class="wrapper-world content">
		<div class="wrapper">
			<h1>Time Based Bus Services</h1> 
			
			<div class="right-half">
				<div class="info-box">
					<div class="info-text" id="formHelper">
						Commonly referred to as "Disposal” service. This is a service provided based on the service duration. [IE. bus is hired from 3pm - 6pm on a given day] The price is charge by the hour. There is a minimum booking requirement of at least 3 hours. Disposal is commonly used for itinerarized travel or event based booking where there are either multiple destinations involved or where there is a need to have the bus on standby at location. In short, the bus is put at the customers’ disposal for entire duration of booking. Please fill in the fields on the left to get a quotation.
					</div>
				</div>
			</div>
			
			<div class="left-half">
				<?php echo form_open('request/ajaxAdd', array('class' => 'standard-form', 'id' => 'ajaxAdd')); ?>
					<div class="one">
						<label>Start Date and Time</label>
						<div class="type-text"><input type="text" name="startdate" id="startdate" /></div>
						<div class="clr"></div>
					</div>
					
					<div class="one">
						<label>End Date and Time</label>
						<div class="type-text"><input type="text" name="enddate" id="enddate" disabled="disabled" /></div>
						<div class="clr"></div>
					</div>
					<div class="one">
						<label>Number of Passengers</label>
						<div class="type-text"><input type="text" name="passangers" /></div>
						<div class="clr"></div>
					</div>
					<div class="one">
						<label>Number of Hours</label>
						<div class="type-text"><input type="text" name="hours" readonly="readonly" /></div>
						<div class="clr"></div>
					</div>
					<div class="one" id="locationsContainer">
						<label>Locations</label>
						<div class="type-text"><input type="text" name="locations" /></div>
						<a href="javascript:addTripLocation();" class="add-remove"><img src="<?php echo site_url(); ?>assets/images/plus.png" /></a>
						<div class="clr"></div>
					</div>
					<div id="after"></div>
					<!-- Form help -->
					<script type="text/javascript">
						helpTexts['default'] = 'Commonly referred to as "Disposal” service. This is a service provided based on the service duration. [IE. bus is hired from 3pm - 6pm on a given day] The price is charge by the hour. There is a minimum booking requirement of at least 3 hours. Disposal is commonly used for itinerarized travel or event based booking where there are either multiple destinations involved or where there is a need to have the bus on standby at location. In short, the bus is put at the customers’ disposal for entire duration of booking. Please fill in the fields on the left to get a quotation.';
						helpTexts['startdate'] = 'Start Date and Time<ul><li>Please enter the start date and time<li>This will denote the reporting time of the bus at your First pick up location as well as the start of your booking window</li></ul>';
						helpTexts['enddate'] = 'End Date and Time<ul><li>Please enter the end date and time</li><li>This will denote the conclusion of your booking window</li><li>Please ensure that the journey to your final destination is within your booking window.</li></ul>';
						helpTexts['passangers'] = 'Number of Passengers<ul><li>Please indicate your number of passengers to aid us in allocating the most suitable bus type</li></ul>';
						helpTexts['hours'] = 'Number of Hours<ul><li>This will denote the service duration / booking window</li></ul>';
						helpTexts['locations'] = 'Locations<ul><li>Please enter your pick up location and destination/s and press the plus button [+] to add them</li><li>Please use the [+] plus button for additional locations</li><li>Alternatively, if you have an itinerary document, you may wish to upload that file via our Upload Itinerary Link.</li></ul><div style="color:red;">For locations within Singapore only.</div>';
		
						initFormHelper('ajaxAdd', 'formHelper');
					</script>
					<input type="hidden" name="type" value="time" />
					<input type="hidden" name="edit_id" value="" id="edit_id" />
				</form>
			</div>
			
			<div class="clr"></div>
		</div>
	</div> <!-- end of wrapper-world and .content -->
	
	<div class="wrapper-world add-btn">
		<div class="wrapper" id="addBtnCont">
			<a href="javascript:addJobTime();" title="Add this job order">ADD</a>
			<div class="add-info">If you have completed your addition, please click <strong>next</strong> located at the bottom of the page.</div>
			<div class="clr"></div>
		</div>
		
		<div class="wrapper" id="editBtnCont" style="display:none;">
			<a href="javascript:addJobTime();" title="Edit this job order" class="apply-btn">APPLY</a> <a href="javascript:cancelEdit();" title="Add this job order" class="cancel-btn">CANCEL</a>
			<div class="clr"></div>
		</div>
	</div>
	
	<div class="job-listing">
		<?php $c = 0; foreach ($jobs as $job): ?>
		<div class="wrapper-world job-<?php echo (($c%2 == 0) ? "odd" : "even"); ?>" id="job_<?php echo $job->id; ?>">
			<div class="wrapper">
				<?php if ($job->job_type == 1): ?>
				<div class="job-type time-based">Time Based</div>
				<?php elseif ($job->job_type == 0): ?>
				<div class="job-type trip-based">Trip Based</div>
				<?php endif; ?>
				<div class="job-details"><?php echo nl2br($this->Jobs->formatJobInfo($job)); ?></div>
				<?php if ($job->job_type <= 1): ?>
				<div class="job-edit"><a href="javascript:editJobTime(<?php echo $job->id; ?>);" title="Edit job order">Edit</a></div>
				<div class="job-delete"><a href="javascript:deleteJob(<?php echo $job->id; ?>);" title="Delete job order">Delete</a></div>
				<?php else: ?>
				<div class="job-edit">&nbsp;</div>
				<div class="job-delete">&nbsp;</div>
				<?php endif; ?>
				<div class="clr"></div>
			</div>
			<?php
				if ($job->job_type == 0) {
					$job->from = htmlspecialchars($job->from, ENT_QUOTES);
					$aLocs = json_decode($job->to);
					for ($ji = 0; $ji < count($aLocs); $ji++)
						$aLocs[$ji] = htmlspecialchars($aLocs[$ji], ENT_QUOTES);
					$job->to = json_encode($aLocs);
				} elseif ($job->job_type == 1) {
					$aLocs = json_decode($job->locations);
					for ($ji = 0; $ji < count($aLocs); $ji++)
						$aLocs[$ji] = htmlspecialchars($aLocs[$ji], ENT_QUOTES);
					$job->locations = json_encode($aLocs);
				}
			?>
			<input type="hidden" class="job_json" value='<?php echo json_encode($job); ?>' />
		</div>
		<?php $c++; endforeach; ?>
	</div>
	
	<div class="wrapper">
		<div class="conv-left">
			<div class="avatar"></div>
			<div class="mid"><div class="top"><div class="bot">
			<?php echo form_open('request/jobOrder', array('class' => 'left-aligned', 'id' => 'jobOrder')); ?>
				<input type="hidden" name="draft" value="0" />
				<input type="hidden" name="type" value="time" />
				<input type="hidden" name="orderId" id="orderId" value="<?php echo $orderId; ?>" />
				<div class="one">
					<label>I would like to supply the following information:</label>
					<div class="type-textarea"><textarea name="moreinfo" id="moreinfo"></textarea></div>
					<div class="clr"></div>
				</div>
			</form>
			<script type="text/javascript">
				helpTexts['moreinfo'] = 'Please include any additional information / instructions here';
				
				jQuery("#moreinfo").val(helpTexts['moreinfo']);
				
				jQuery("#moreinfo").focus(function() {
					if (jQuery(this).val() == helpTexts['moreinfo']) {
						jQuery(this).val("");
					}
				});
				
				jQuery("#moreinfo").blur(function() {
					if (jQuery(this).val() == "") {
						jQuery(this).val(helpTexts['moreinfo']);
					}
				});
			</script>
			</div></div></div>
			<div class="clr"></div>
		</div>
	</div>
	
	<div class="wrapper">
		<div class="action-buttons">
			<?php
			if ($user->level > 0) {
				if ($orderId > 0) {
					$sendText = 'Transform';
				} else {
					$sendText = 'Add Order';
				}
			}
			?>
			<?php if (!($orderId > 0) && $user->level < 1): ?><a class="more" href="javascript:jobOrder(1);"><span>Save as draft</span></a> <?php endif; ?><a class="more" href="javascript:jobOrder();"><span><?php echo $sendText; ?></span></a>
		</div>
	</div>
	
	<div class="wrapper-world footer-top">
		<div class="wrapper"></div>	
	</div>
	
	<?php if ($editId > 0): ?>
	<script type="text/javascript">
		jQuery(document).ready(function() { editJobTime(<?php echo $editId; ?>); });
	</script>
	<?php endif; ?>
	
	<?php $this->load->view('footer'); ?>
</body>
</html>
